package org.bot.server.socket.impl;

import com.alibaba.fastjson.JSONObject;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Set;

@Component
public class HeartbeatMonitor implements Job {

    private static ServerWebSocket webSocket;

    @Autowired
    public void setWebSocket(ServerWebSocket socket){
        webSocket = socket;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        final Set<String> keys = webSocket.clientSession.keySet();
        keys.forEach(k->{
            WebSocketSession webSocketSession = webSocket.clientSession.get(k);
            if (webSocketSession == null){
                return;
            }
            final JSONObject message = webSocket.getMessage(MessageType.HEARTBEAT);
            webSocket.sendMessage(message.toString(),webSocketSession);
        });
    }
}
